package com.neusoft.controller;

import com.neusoft.dto.Login;
import com.neusoft.dto.Registration;
import com.neusoft.dto.UserUpdate;
import com.neusoft.model.Users;
import com.neusoft.response.ApiResponse;
import com.neusoft.service.IUserService;
import com.neusoft.utils.JwtService;
import com.neusoft.utils.UserUtils;
import com.neusoft.vo.RegUserVo;
import com.neusoft.vo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@RestController
@RequestMapping("/api")
public class UserController {
    @Resource
    IUserService iUserService;
    @Autowired
    JwtService jwtService;

    @PostMapping("/users")
    public ApiResponse<RegUserVo> registration(@RequestBody Registration registration) {
        return ApiResponse.success(iUserService.registerUser(registration.getUser()));
    }

    @PostMapping("/users/login")
    public ApiResponse<RegUserVo> login(@RequestBody Login login) {
       return ApiResponse.success(iUserService.login(login.getUser()));
    }
    @GetMapping("/user")
    public ApiResponse<RegUserVo> getUsers() {
        Users currentUser = UserUtils.getLoginUser();
        User user = new User();
        user.setImage(currentUser.getImage());
        user.setBio(currentUser.getBio());
        user.setUsername(currentUser.getUsername());
        user.setEmail(currentUser.getEmail());
        String token = jwtService.toToken(currentUser);
        user.setToken(token);
        RegUserVo regUserVo = new RegUserVo();
        regUserVo.setUser(user);
        return ApiResponse.success(regUserVo);
    }
    @PutMapping("/user")
    public ApiResponse<RegUserVo> updateUser(@RequestBody UserUpdate userUpdate) {
         return ApiResponse.success(iUserService.updateUser(userUpdate));
    }

}
